﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using T.ApiResults;
using T.Materials.Domain.Dto;
using T.Materials.Domain.Model;
using T.Materials.Read.API.ApplicationRead.Command.MaterialRequisitionCommand;
using T.Materials.Read.API.ApplicationRead.Command.MaterialStoreCommand;

namespace T.Materials.Read.API.Controllers
{
    /// <summary>
    /// API控制器--物资领用
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ShwoMaterialRequisitionController : ControllerBase
    {
        /// <summary>
        /// 命令总线
        /// </summary>
        protected readonly IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">命令总线</param>
        public ShwoMaterialRequisitionController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 查询物资领用
        /// </summary>
        /// <param name="command">查询物资领用命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpGet]
        public async Task<ApiResult<ListMaterialRequisitionDto<MaterialRequisition>>> GetMaterialRequisitionList([FromQuery] ShowMaterialRequisitionCommand command)
        {
            ApiResult<ListMaterialRequisitionDto<MaterialRequisition>> result = new ApiResult<ListMaterialRequisitionDto<MaterialRequisition>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "查询物资领用失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填物资领用
        /// </summary>
        /// <param name="command">反填物资领用命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpGet]
        public async Task<ApiResult<MaterialRequisition>> ReverseMaterialRequisition([FromQuery] ReverseMaterialRequisitionCommand command)
        {
            ApiResult<MaterialRequisition> result = new ApiResult<MaterialRequisition>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "新增物资领用失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 查询存储物资领用存储
        /// </summary>
        /// <param name="command">反填物资领用命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpGet]
        public async Task<ApiResult<List<StorageMaterialsDto>>> ReverseStorageMaterials([FromQuery] ShowCollectingItemsCommand command)
        {
            ApiResult<List<StorageMaterialsDto>> result = new ApiResult<List<StorageMaterialsDto>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "新增物资领用失败";
                throw;
            }
            return result;
        }
    }
}
